﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace PersonListManager
{
    public partial class MainForm : Form
    {
        private BindingList<Person> personList;
        private BindingList<Person> searchResult;

        public MainForm()
        {
            InitializeComponent();

            InitializePersonList();
        }

        private void InitializePersonList()
        {
            this.personList = new BindingList<Person>();
            this.searchResult = new BindingList<Person>();

            this.personList.Add(new Person("Ashot", "Ashotyan"));
            this.personList.Add(new Person("Bagrat", "Bagratyan"));

            for (int i = 0; i < 50; i++)
            {
                this.personList.Add(new Person("John" + " " + i, "Smith" + " " + i));
            }

            listBoxPersons.DataSource = personList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string firstName = textBoxFirstName.Text.Trim();
            string lastName = textBoxLastName.Text.Trim();

            Person person = new Person(firstName, lastName);

            this.personList.Add(person);

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int currentIndex = listBoxPersons.SelectedIndex;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            string term = textBoxSearch.Text.Trim();

            if (term == String.Empty)
            {
                listBoxPersons.DataSource = personList;
            }
            else
            {
                searchResult.Clear();

                foreach (Person person in this.personList)
                {
                    if (person.FullName.Contains(term))
                    {
                        searchResult.Add(person);
                    }
                }

                listBoxPersons.DataSource = searchResult;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveDialog.RestoreDirectory = true;

            //string data = String.Empty;
            StringBuilder sb = new StringBuilder();

            BindingList<Person> test;

            if (searchResult.Count != 0)
            {
                test = this.searchResult;
            }
            else
            {
                test = this.personList;
            }

            foreach (Person person in test)
            {
                sb.AppendLine(String.Format("{0}\t{1}",person.FullName, person.Age));
            }

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(saveDialog.FileName, sb.ToString());
            }


        }
    }
}
